﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace DNFImagePacks2Manager.Helpers
{
    public sealed class BKHelper
    {
        // BK套装文件夹
        private readonly static string wholeFolderName = "whole";
        // BK配置文件夹
        private readonly static string customFolderName = "custom";
        // BK单品文件夹
        private readonly static string singleFolderName = "single";

        public static int GetType(string filePath)
        {
            try
            {
                using FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                byte[] header = new byte[3];
                fs.Read(header, 0, 3);

                string magic = ByteHelper.Bytes2AsciiString(header);

                if (magic == "BIK")
                {
                    return 1;
                }
                else if (magic == "KB2")
                {
                    return 2;
                }
                header = new byte[16];
                fs.Position = 0;
                fs.Read(header, 0, 16);
                magic = ByteHelper.Bytes2AsciiString(header);
                if (magic == "Neople Video Fil")
                {
                    return 0;
                }
            }
            catch (Exception) {}
            return -1;
        }

        public static bool SaveBKFile(string file, out string message)
        {
            string folder = GetSingleFolder();
            string targetFile = Path.Combine(folder, Path.GetFileName(file));
            if (File.Exists(targetFile)) {
                message = "有同名文件已存在";
                return false;
            }
            else
            {
                try
                {
                    File.Copy(file, targetFile);
                    message = "";
                    return true;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    return false;
                }
            }
        }

        public static bool ChangeBKStoragePath(string newPath)
        {
            if (Directory.Exists(newPath)) {
                try
                {
                    string wholeFolder = GetWholeFolder();
                    if (Directory.Exists(wholeFolder) && Directory.EnumerateFileSystemEntries(wholeFolder).Any())
                    {
                        DirectoryHelper.MoveDirectory(wholeFolder, Path.Combine(newPath, wholeFolderName));
                    }
                    string singleFolder = GetSingleFolder();
                    if (Directory.Exists(singleFolder) && Directory.EnumerateFileSystemEntries(singleFolder).Any())
                    {
                        DirectoryHelper.MoveDirectory(singleFolder, Path.Combine(newPath, singleFolderName));
                    }
                    return true;
                }
                catch (Exception) {
                }
            }
            return false;
        }

        public static string GetBKStorageFolder()
        {
            string bkFolder = LocalStorageHelper.GetBKStoragePath();
            if (!Directory.Exists(bkFolder))
            {
                Directory.CreateDirectory(bkFolder);
            }
            return bkFolder;
        }

        public static string GetWholeFolder()
        {
            string wholeFolder = Path.Combine(GetBKStorageFolder(), wholeFolderName);
            if (!Directory.Exists(wholeFolder))
            {
                Directory.CreateDirectory(wholeFolder);
            }
            return wholeFolder;
        }

        public static string GetSingleFolder() {
            string singleFolder = Path.Combine(GetBKStorageFolder(), singleFolderName);
            if (!Directory.Exists(singleFolder))
            {
                Directory.CreateDirectory(singleFolder);
            }
            return singleFolder;
        }

        public static string GetCustomFolder()
        {
            string customFolder = Path.Combine(GetBKStorageFolder(), customFolderName);
            if (!Directory.Exists(customFolder))
            {
                Directory.CreateDirectory(customFolder);
            }
            return customFolder;
        }
    }
}
